﻿Imports System.Text
Imports System.Web

Namespace DotNetNuke.Modules.ActiveForums.Controls
    Public Class ForumDirectory
        Public Property PortalId() As Integer = -1
        Public Property ModuleId() As Integer = -1
        Public Property TabId() As Integer = -1
        Public Property ForumUser() As User
        Public Property ForumIds() As String = String.Empty
        Public Property Template() As String = String.Empty

        Public Function Render() As String
            If String.IsNullOrEmpty(Template) Then
                Return "Please specify a template"
            End If
            Dim sb As New StringBuilder
            Dim fdb As New Data.ForumsDB
            Dim allForums As ForumCollection = fdb.Forums_List(PortalId, ModuleId)
            Dim filteredForums As New ForumCollection
            For Each f As Forum In allForums
                If f.ForumGroup.Active And f.Active And f.ParentForumId = 0 Then
                    If Permissions.HasPerm(f.Security.View, ForumUser.UserRoles) Then
                        f.TabId = TabId
                        f.SubForums = GetSubForums(allForums, f.ForumID)
                        filteredForums.Add(f)
                    End If
                End If
            Next
            Dim groupTemplate As String = TemplateUtils.GetTemplateSection(Template, "[AF:DIR:FORUMGROUP]", "[/AF:DIR:FORUMGROUP]")
            Dim forumTemplate As String = TemplateUtils.GetTemplateSection(Template, "[AF:DIR:FORUM]", "[/AF:DIR:FORUM]")
            Dim subForumTemplate As String = TemplateUtils.GetTemplateSection(Template, "[AF:DIR:SUBFORUM]", "[/AF:DIR:SUBFORUM]")
            Dim currGroup As Integer = -1
            Dim gtmp As String = String.Empty
            Dim ftmp As String = String.Empty
            Dim subtmp As String = String.Empty
            Dim list As New StringBuilder()
            Dim inprogress As Boolean = False
            For Each f As Forum In filteredForums
                If currGroup <> f.ForumGroupId Then
                    If Not String.IsNullOrEmpty(gtmp) Then
                        gtmp = gtmp.Replace("[FORUMHOLDER]", String.Empty)
                        list.Append(gtmp)
                    End If
                    gtmp = groupTemplate
                    gtmp = TemplateUtils.ReplaceSubSection(gtmp, "[FORUMHOLDER]", "[AF:DIR:FORUM]", "[/AF:DIR:FORUM]")
                    gtmp = ParseForumGroup(f.ForumGroup, gtmp)
                    ftmp = forumTemplate
                    ftmp = TemplateUtils.ReplaceSubSection(ftmp, "[SUBFORUMHOLDER]", "[AF:DIR:SUBFORUM]", "[/AF:DIR:SUBFORUM]")
                    subtmp = subForumTemplate
                    currGroup = f.ForumGroupId
                End If
                Dim forums As String = ParseForum(f, ftmp)
                If Not f.SubForums Is Nothing Then
                    For Each s As Forum In f.SubForums
                        forums = forums.Replace("[SUBFORUMHOLDER]", ParseForum(s, subtmp) & "[SUBFORUMHOLDER]")
                    Next
                End If
                forums = forums.Replace("[SUBFORUMHOLDER]", String.Empty)
                gtmp = gtmp.Replace("[FORUMHOLDER]", forums & "[FORUMHOLDER]")

            Next
            gtmp = gtmp.Replace("[FORUMHOLDER]", String.Empty)
            list.Append(gtmp)
            Template = TemplateUtils.ReplaceSubSection(Template, list.ToString, "[AF:DIR:FORUMGROUP]", "[/AF:DIR:FORUMGROUP]")
            Return Template
        End Function
        Private Function ParseForumGroup(ByVal f As ForumGroupInfo, ByVal template As String) As String
            template = template.Replace("[AF:DIR:FORUMGROUPID]", f.ForumGroupId.ToString)
            template = template.Replace("[AF:DIR:FORUMGROUPNAME]", f.GroupName)
            Return template
        End Function
        Private Function ParseForum(ByVal f As Forum, ByVal template As String) As String
            template = template.Replace("[AF:DIR:FORUMID]", f.ForumID.ToString)
            template = template.Replace("[AF:DIR:FORUMNAME]", f.ForumName)
            template = template.Replace("[AF:DIR:FORUMDESC]", f.ForumDesc)
            template = template.Replace("[AF:DIR:FORUMURL]", f.ForumURL)
            template = template.Replace("[AF:DIR:TOTALTOPICS]", f.TotalTopics.ToString)
            template = template.Replace("[AF:DIR:TOTALREPLIES]", f.TotalReplies.ToString)
            template = template.Replace("[AF:DIR:FORUMGROUPID]", f.ForumGroupId.ToString)
            template = template.Replace("[AF:DIR:PARENTFORUMID]", f.ParentForumId.ToString)
            Dim selected As String = String.Empty
            If Not HttpContext.Current.Request.QueryString(ParamKeys.ForumId) Is Nothing Then
                If Integer.Parse(HttpContext.Current.Request.QueryString(ParamKeys.ForumId)) = f.ForumID Then
                    selected = "afn-currentforum"
                Else
                    selected = String.Empty
                End If
            End If
            If f.ParentForumId > 0 Then
                template = template.Replace("[AF:DIR:SELECTEDSUBFORUM]", selected)
            Else
                template = template.Replace("[AF:DIR:SELECTEDFORUM]", selected)
            End If

            Return template
        End Function
        Private Function GetSubForums(ByVal forums As ForumCollection, ByVal forumId As Integer) As ForumCollection
            Dim subforums As ForumCollection = Nothing
            For Each s As Forum In forums
                If s.ParentForumId = forumId Then
                    If subforums Is Nothing Then
                        subforums = New ForumCollection
                    End If
                    s.TabId = TabId
                    subforums.Add(s)
                End If
            Next
            Return subforums
        End Function
    End Class
End Namespace
